package com.fzy.common.propertie;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.texen.util.PropertiesUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @desc 通过spring读取项目配置文件(* .properties)
 */
public class PropertieUtil extends PropertyPlaceholderConfigurer {
    private PropertieUtil() {
    }

    private static Map<String, Object> ctxPropertiesMap;

    private static Properties properties = null;

    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactoryToProcess,
            Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        ctxPropertiesMap = new HashMap<>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }

        properties = props;
    }

    public static Object getProperty(String key) {
        return ctxPropertiesMap.get(key);
    }

    public static int getProperty(String key, int defalut) {
        Object value = getProperty(key);
        if (value == null) {
            return defalut;
        }
        Integer i;
        try {
            i = Integer.parseInt(value.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return defalut;
        }
        return i;
    }


    public static boolean setProperty(String key, String value) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
            return false;
        }
        properties.setProperty(key, value);
        return true;
    }

    /**
     * 根据key读取value
     *
     * @param fileName:属性文件名
     * @param key:属性名
     * @return:属性值value
     */
    public static String read(String fileName, String key) {
        Properties props = new Properties();
        try {

            props.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
            String n = props.getProperty(key);
            System.out.println(n);
            return n;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 读取properties 的全部信息
     *
     * @param fileName:属性文件名 放在工程的src目录下
     */
    public static void readProperties(String fileName) {
        Properties props = new Properties();
        try {
            props.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
            Enumeration<?> en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String Property = props.getProperty(key);
                System.out.println(key + ":" + Property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入properties信息
     *
     * @param fileName:属性文件名
     * @param parameterName:属性名
     * @param parameterValue:属性值
     */
    public static void writeProperties(String fileName, String parameterName, String parameterValue) {
        Properties prop = new Properties();

        String path = PropertiesUtil.class.getClassLoader().getResource("").getPath().substring(1);
        path = path + fileName;
        System.out.println(fileName);
        try {
            InputStream fis = new FileInputStream(path);
            //从输入流中读取属性列表(键和元素对)
            prop.load(fis);
            //调用Hashtable的方法put(),使用getProperty()方法提供并行性
            //强制要求为属性的键和值使用字符串，返回值是Hashtable调用put的结果
            OutputStream fos = new FileOutputStream(path);
            prop.setProperty(parameterName, parameterValue);
            //以适合使用load方法加载到properties表中的格式
            //将此Properties表中的属性列表（键和元素对）写入输出流
            prop.store(fos, "Update" + parameterName + "value");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}